﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.Model;
using System.Collections;

namespace WXYW
{
    public partial class ComponentQuery : System.Web.UI.Page
    {
        public int RecordsNumber { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ServiceStationService stationService = new ServiceStationService();
                platformDropdownList.DataSource = stationService.GetAllWithSelectOne();
                platformDropdownList.DataTextField = "Name";
                platformDropdownList.DataValueField = "ID";
                platformDropdownList.DataBind();
            }
        }

        protected void searchButton_Click(object sender, EventArgs e)
        {
            AccessoryService service = new AccessoryService();
            AccessoryListGridView.DataSource = service.QueryAccessory(keywordTxtBox.Text, Convert.ToInt32(platformDropdownList.SelectedValue));
            AccessoryListGridView.DataBind();
        }

        protected void AccessoryListGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }


            if (e.Row.RowType == DataControlRowType.Pager)
            {
                DropDownList pageDDL = e.Row.FindControl("pageDDL") as DropDownList;
                for (int index = 1; index <= AccessoryListGridView.PageCount; index++)
                {
                    pageDDL.Items.Add(index.ToString());
                }
                pageDDL.SelectedValue = (AccessoryListGridView.PageIndex + 1).ToString();
            }
        }

        protected IEnumerable GridView_MustAddARow(IEnumerable data)
        {
            List<AccessoryModel> dds = new List<AccessoryModel>();
            dds.Add(new AccessoryModel());
            return dds;
        }

        protected void pageDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList pageDDL = sender as DropDownList;
            AccessoryListGridView.PageIndex = Convert.ToInt32(pageDDL.SelectedIndex);
            AccessoryService service = new AccessoryService();
            AccessoryListGridView.DataSource = service.QueryAccessory(keywordTxtBox.Text, Convert.ToInt32(platformDropdownList.SelectedValue));
            AccessoryListGridView.DataBind();
        }

        protected void AccessoryListGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (e.NewPageIndex >= 0 && e.NewPageIndex < AccessoryListGridView.PageCount)
            {
                AccessoryListGridView.PageIndex = e.NewPageIndex;
            }

            AccessoryService service = new AccessoryService();
            AccessoryListGridView.DataSource = service.QueryAccessory(keywordTxtBox.Text, Convert.ToInt32(platformDropdownList.SelectedValue));
            AccessoryListGridView.DataBind();
        }
    }
}
